local skels = {}

local gushou = fk.CreateSkill {
  name = "xiaobai__gushou"
}
Fk:loadTranslationTable {
  ["xiaobai__gushou"] = "诂守",
  [":xiaobai__gushou"] = "①伤害牌②锦囊牌③K点牌进入弃牌堆前，你的♠基本牌仅能当做首项的基本牌或普通锦囊牌使用，" ..
      "首项牌进入弃牌区时删去之。均删去后，你分配弃牌堆中上述项牌X张（X为你以此法使用牌数）。",
  ["#xiaobai__gushou"] = "诂守：将♠基本牌当首项即时牌使用",
  ["@[xiaobai__gushou]"] = "诂守",
  ["#xiaobai__gushou-gain"] = "诂守：分配弃牌堆中上述项牌%arg张",
  ["xiaobai__gushou_active"] = "诂守",
}
local U = require "packages.utility.utility"

Fk:addQmlMark {
  name = "xiaobai__gushou",
  how_to_show = function(name, value, player)
    local num = player:getMark(gushou.name)
    local num2 = player:getMark("xiaobai__gushou_number")
    local str = (num < 1 and "<font color='burlywood'>" or "<font color='grey'>") + "伤 </font>"
    local str2 = (num < 2 and (num == 1 and "<font color='burlywood'>" or "<font color='white'>") or "<font color='grey'>") +
        "锦 </font>"
    local str3 = (num < 3 and (num == 2 and "<font color='burlywood'>" or "<font color='white'>") or "<font color='grey'>") +
        "K </font>"
    return str + str2 + str3 + (num2 ~= 0 and num2 or "")
  end,
  qml_path = " ",
}
gushou:addEffect("viewas", {
  pattern = ".",
  prompt = "#xiaobai__gushou",
  interaction = function(self, player)
    local all_names = {}
    for _, name in ipairs(Fk:getAllCardNames("bt")) do
      local c = Fk:cloneCard(name)
      local num = player:getMark(gushou.name)
      if (num == 0 and c.is_damage_card) or (num == 1 and c:isCommonTrick()) or (num == 2 and table.find(Fk.cards, function(
              card)
            return card.package and not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and
                not card.is_derived and card.number == 13 and card.name == name
          end)) then
        table.insertIfNeed(all_names, name)
      end
    end
    local names = player:getViewAsCardNames(gushou.name, all_names, nil)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    return self.interaction.data and #selected == 0 and Fk:getCardById(to_select).suit == Card.Spade and
        Fk:getCardById(to_select).type == Card.TypeBasic
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = gushou.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:addPlayerMark(player, "xiaobai__gushou_number")
  end,
  enabled_at_response = function(self, player, response)
    local all_names = {}
    for _, name in ipairs(Fk:getAllCardNames("bt")) do
      local c = Fk:cloneCard(name)
      local num = player:getMark(gushou.name)
      if (num == 0 and c.is_damage_card) or (num == 1 and c:isCommonTrick()) or (num == 2 and table.find(Fk.cards, function(
              card)
            return card.package and not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and
                not card.is_derived and card.number == 13 and card.name == name
          end)) then
        table.insertIfNeed(all_names, name)
      end
    end
    local names = player:getViewAsCardNames(gushou.name, all_names, nil)
    return not response and #names > 0
  end,
})
gushou:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    local num = player:getMark(gushou.name)
    if player:hasSkill(gushou.name) and num < 3 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if num == 0 and Fk:getCardById(info.cardId).is_damage_card then
              return true
            end
            if num == 1 and Fk:getCardById(info.cardId).type == Card.TypeTrick then
              return true
            end
            if num == 2 and Fk:getCardById(info.cardId).number == 13 then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local nums = {}
    local num = player:getMark(gushou.name)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if not table.contains(nums, 0) and Fk:getCardById(info.cardId).is_damage_card then
            table.insert(nums, 0)
          end
          if not table.contains(nums, 1) and Fk:getCardById(info.cardId).type == Card.TypeTrick then
            table.insert(nums, 1)
          end
          if not table.contains(nums, 2) and Fk:getCardById(info.cardId).number == 13 then
            table.insert(nums, 2)
          end
        end
      end
    end
    for i = 0, 2, 1 do
      if num == i and table.contains(nums, i) then
        room:addPlayerMark(player, gushou.name)
      end
    end
    local number = player:getMark("xiaobai__gushou_number")
    if player:getMark(gushou.name) > 2 and number > 0 then
      local cards = {}
      for index, cardID in ipairs(room.discard_pile) do
        local card = Fk:getCardById(cardID)
        if card.is_damage_card or card.type == Card.TypeTrick or card.number == 13 then
          table.insertIfNeed(cards, cardID)
        end
      end
      if #cards > 0 then
        local gains = cards
        if #cards > number then
          gains = room:askToChooseCards(player, {
            target = player,
            min = number,
            max = number,
            flag = { card_data = { { "pile_discard", cards } } },
            pattern = ".",
            skill_name = gushou.name,
            prompt = "#xiaobai__gushou-gain:::" .. U.ConvertNumber(number)
          })
        end
        --[[room:askToYiji(player, {
                    cards = gains,
                    min_num = 0,
                    max_num = #gains,
                    skill_name = gushou.name,
                    prompt = "#xiaobai__gushou-gain:::"..U.ConvertNumber(number)
                })]] --
        while true do
          room:setPlayerMark(player, "xiaobai__gushou_cards", gains)
          local _, ret = room:askToUseActiveSkill(player, {
            skill_name = "xiaobai__gushou_active",
            prompt = "#xiaobai__gushou-gain:::" .. U.ConvertNumber(number),
            cancelable = true,
            no_indicate = true,
          })
          room:setPlayerMark(player, "xiaobai__gushou_cards", 0)
          if ret then
            for _, id in ipairs(ret.cards) do
              table.removeOne(gains, id)
            end
            room:moveCardTo(ret.cards, Card.PlayerHand, ret.targets[1], fk.ReasonGive, "xiaobai__gushou", nil,
              false, player)
            if #gains == 0 then break end
            if player.dead then
              room:moveCards({
                ids = gains,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonJustMove,
                skillName = "xiaobai__gushou",
              })
              break
            end
          else
            room:moveCardTo(gains, Player.Hand, player, fk.ReasonGive, "xiaobai__gushou", nil, false, player)
            break
          end
        end
      end
    end
  end,
})
gushou:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[xiaobai__gushou]", 1)
end)
gushou:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[xiaobai__gushou]", 0)
end)
gushou:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if not player:hasSkill(gushou.name) or player:getMark(gushou.name) > 2 or not card or card.suit ~= Card.Spade or card.type ~= Card.TypeBasic or #card.skillNames > 0 then return end
    local subcards = Card:getIdList(card)
    return #subcards > 0 and table.every(subcards, function(id)
      return table.contains(player:getHandlyIds(), id)
    end)
  end,
})
table.insert(skels, gushou)

local gushouActive = fk.CreateSkill {
  name = "xiaobai__gushou_active",
}
gushouActive:addEffect("active", {
  expand_pile = function(self, player)
    return player:getTableMark("xiaobai__gushou_cards")
  end,
  min_card_num = 1,
  target_num = 1,
  card_filter = function(self, player, to_select, selected)
    return table.contains(player:getTableMark("xiaobai__gushou_cards"), to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
})
table.insert(skels, gushouActive)

return skels
